#include <assert.h>
#include <QDebug>
#include "puck_drawer.h"

void PuckDrawer::init() {
    assert(m_vbo);
    assert(m_vao);
    // 顶点数据搬运至显存, 顶点为三维坐标
    m_vbo->allocate(m_puck->getVertexData(), m_puck->getVertexPointSize() * 3 * sizeof(float));
    // 设置属性
    m_program->setAttribute(m_program->getAttrPosition(), 3, 3 * sizeof(float), 0);
    // enable属性
    m_program->enableAttribute(m_program->getAttrPosition());
}

void PuckDrawer::draw(QMatrix4x4 &modelViewProjectM) {
    // 绘制图形
    m_vao->bind();
    m_program->bind();
    // 设置变换矩阵
    glUniformMatrix4fv(m_program->getUniformProjMatrix()
            , 1
            , false
            , modelViewProjectM.data());
    int uColorLoc = static_cast<SimpleShaderProgram *>(m_program)->getUniformColor();
    // 圆筒
    QVector4D cylinderC = m_puck->getCylinderColor();
    glUniform4f(uColorLoc, cylinderC.x(), cylinderC.y(), cylinderC.z(), cylinderC.w());
    glDrawArrays(GL_TRIANGLE_STRIP, m_puck->getTopCirclePointSize(), m_puck->getCylinderPointSize());
    // 上部圆盘
    QVector4D circleC = m_puck->getCircleColor();
    glUniform4f(uColorLoc, circleC.x(), circleC.y(), circleC.z(), circleC.w());
    glDrawArrays(GL_TRIANGLE_FAN, 0, m_puck->getTopCirclePointSize());
}
